package com.heima.kafka.consumer;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class HelloListener {


   /* @KafkaListener(topics = "itcast-topic")
    public void onMessage(String message){
        if(!StringUtils.isEmpty(message)){
            System.out.println(message);
        }
    }*/

    /**
     * kafka消费者
     * @param record
     * @param consumer
     */
    @KafkaListener(topics = "itcast-topic")
    public void onMessage(ConsumerRecord<String, String> record, Consumer<String,String> consumer){
        log.info("消费者开启");
        if(!StringUtils.isEmpty(record)){
            //获取传递值
            System.out.println(record.value());
            //获取传递偏移量
            System.out.println(record.offset());
            //提交偏移量
            consumer.commitSync();
        }
    }

    /**
     * 下架文章
     * @param message
     */
    @KafkaListener(topics = "leadnews-wemedia")
    public void onMessage(String message){
        if(!StringUtils.isEmpty(message)){
            log.info("下架app端文章");
            Map map = JSON.parseObject(message, HashMap.class);
            log.info("解析后数据:{}",map);
        }
    }


}